﻿from google.appengine.ext import webapp
from utilities import *
from entities import *
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp 
from google.appengine.api import users      
from entities import Entities     
                       
import os

class Form(webapp.RequestHandler):
    def get(self):
        #DoiTuong.Add_Admin(u"Người khiếm thị",u"khiếm thị",u"Người không có khả năng nhìn thấy")
        #status = DoiTuongNguoiDung.Add("aghsb25ndGRia3IPCxIJTmd1b2lEdW5nGEUM",99)
        #status = Entities.NguoiDung.get_by_id(69)
        #status = status.TenNguoiDung
        #TinhThanh.Add("Hanoi")
        #CapBac.Add("Giam doc")
        user = users.get_current_user()
        
        bool = 0
        TenVaiTro = None
        template_values = {
                'DoiTuongs':0,
                'menu' : HtmlTemplate.Menu(),
                'footer': HtmlTemplate.Footer()   
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/RegisterDeveloper.html')
        if user:  # signed in already
            vaitro = NguoiDungEntities.Get(user)
            if vaitro:
                vaitro_ID = vaitro.key().id()
                VaiTros = VaiTroEntities.Get(vaitro_ID)
                for vaitro in VaiTros:
                    TenVaiTro = vaitro['TenVaiTro']
                if TenVaiTro == "Admin":
                    
                    DoiTuongs = DoiTuongEntities.All()
                    LinhVucs = LinhVucEntities.All()
                    NganhNghes = NganhNgheEntities.All()
                    NhaTuyenDungs = NhaTuyenDungEntities.All()
                    TrangThais = TrangThaiEntities.All()
                    DonDangTuyens = DonDangTuyenEntities.All()
                    template_values = {
                        'DoiTuongs':DoiTuongs,
                        'LinhVucs':LinhVucs,
                        'NganhNghes':NganhNghes,
                        'NhaTuyenDungs': NhaTuyenDungs,
                        'TrangThais': TrangThais,
                        'DonDangTuyens' : DonDangTuyens,
                        'menu' : HtmlTemplate.Menu(),
                        'footer': HtmlTemplate.Footer()                        
                    }
                
                    path = os.path.join(os.path.dirname(__file__), '../templates/AdminSite.html')
                
        else:
            self.redirect(users.create_login_url(self.request.uri))

        self.response.out.write(template.render(path, template_values))
        
    def post(self):
        TenUngDung = self.request.get('txtApp')
        user = users.get_current_user()
        vaitro = VaiTroEntities.GetID("Developer")
        status = NguoiDungEntities.Add(user,TenUngDung,vaitro)
        
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()
        }
        
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
       
class View(webapp.RequestHandler):
    def post(self):
        #user = users.get_current_user()
        #keyNguoiDung = str(NguoiDung.GetKey(user))
        TenDoiTuong = self.request.get('name')
        MoTaNgan = self.request.get('shortdes')
        MoTaDai = self.request.get('longdes')
        #try:
        #nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
        #except:
         #   return "Invalid Key"
        status = DoiTuongEntities.Add_Admin(TenDoiTuong,MoTaNgan,MoTaDai)
        template_values = {
            'status': status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
class Edit(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        keyNguoiDung = str(NguoiDungEntities.GetKey(user))
        doituongID = int(self.request.get('eid'))
        TenDoiTuong = self.request.get('ename')
        MoTaNgan = self.request.get('eshortdes')
        MoTaDai = self.request.get('elongdes')
        status = DoiTuongEntities.Update(keyNguoiDung,doituongID,TenDoiTuong,MoTaNgan,MoTaDai)
        template_values = {
            'status': status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
class EditLinhVuc(webapp.RequestHandler):
    def post(self):
        #user = users.get_current_user()
        #keyNguoiDung = str(NguoiDung.GetKey(user))
        LinhVucID = int(self.request.get('eLVid'))
        TenLinhVuc = self.request.get('eLVname')
        status = LinhVucEntities.Update(LinhVucID,TenLinhVuc)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()  
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
class CreateLinhVuc(webapp.RequestHandler):
    def post(self):
        #user = users.get_current_user()
        #keyNguoiDung = str(NguoiDung.GetKey(user))
        TenLinhVuc = self.request.get('LVname')
        status = LinhVucEntities.Add_Admin(TenLinhVuc)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()             
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
class EditNganhNghe(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        keyNguoiDung = str(NguoiDungEntities.GetKey(user))
        NganhNgheID = int(self.request.get('eNNid'))
        TenNganhNghe = self.request.get('eNNname')
        LinhVucID = int(self.request.get('eLV_NNid'))
        status = NganhNgheEntities.Update(keyNguoiDung,NganhNgheID,TenNganhNghe,LinhVucID)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()  
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))

class CreateNganhNghe(webapp.RequestHandler):
    def post(self):
        #user = users.get_current_user()
        #keyNguoiDung = str(NguoiDung.GetKey(user))
        TenNganhNghe = self.request.get('NNname')
        LinhVucID = int(self.request.get('ddLV'))
        status = NganhNgheEntities.Add_Admin(TenNganhNghe,LinhVucID)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()             
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
class EditNhaTuyenDung(webapp.RequestHandler):
    def post(self):
        NhaTuyenDungID = int(self.request.get('eNTDid'))
        TrangThaiID = int(self.request.get('ddlTrangThai'))
        status = NhaTuyenDungEntities.UpdateStatus(NhaTuyenDungID,TrangThaiID)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()              
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
class EditDonDangTuyen(webapp.RequestHandler):
    def post(self):
        DonDangTuyenID = int(self.request.get('eDDTid'))
        TrangThaiID = int(self.request.get('ddlTrangThai2'))
        status = DonDangTuyenEntities.UpdateStatus(DonDangTuyenID,TrangThaiID)
        template_values = {
            'status':status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()  
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/AdminSiteView.html')
        self.response.out.write(template.render(path, template_values))
        
       